package com.atguigu.gmall.cart.rpc.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.bo.UserIdBo;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/api/inner/cart")
@RestController
public class CartController {


    @Autowired
    CartService cartService;

    @GetMapping("/addtocart/{skuId}")
    public Result addSkuToCart(@PathVariable("skuId") Long skuId,
                               @RequestParam("skuNum") Integer skuNum){
        UserIdBo bo = AuthContextHolder.getUserIdBo();
        //web-all发起了对我们的远程调用。但是是创建新请求调用我们，导致没有请求头。
        System.out.println("购物车：userId:"+bo.getUserId());
        System.out.println("购物车：tempId:"+bo.getUserTempId());


        //redis购物车  cart:info:fdjlfjsdkljfsdjfsl
        //1、决定用哪个购物车
        String cartKey = cartService.determinCartKey(bo.getUserId(),bo.getUserTempId());

        //2、把商品添加到购物车，并返回刚才添加的是哪个商品数据
        //3、如果是临时购物车，设置有效期1年；
        SkuInfo skuInfo = cartService.addToCart(cartKey, skuId, skuNum);

        return Result.ok(skuInfo);
    }

}
